﻿#ifndef TCPCLINET_H
#define TCPCLINET_H

#include <QTcpSocket>

class TcpClient : public  QObject
{
    Q_OBJECT
public:
    explicit TcpClient(QObject *parent = nullptr);

    void cleanup();
    bool connectServer(const QHostAddress &ip, int port);
    bool isOpen() { return m_client->isOpen(); }
    bool closeConnect();

    void resetCNT() { m_sendMsgLenCnt = m_revMsgLenCnt = 0; }

    void sendMsgToClient(const QByteArray &msg);

signals:
    void sigIsOnline(bool isOnline);
    void sigReadMsg(const QString &ip, const QByteArray& msg);
    void sigCNTUpdate(int send_len, int rev_len);

    void sigSystemLog(const QString &log_msg);

private slots:
    void onConnected();
    void onDisconnected();
    void onReadyRead();

private:
    int m_sendMsgLenCnt;    ///< 发送的字符长度计数
    int m_revMsgLenCnt;     ///< 收到的字符长度计数

    bool m_isOnline;
    QTcpSocket *m_client;

};

#endif // TCPCLINET_H
